import { _decorator, Button, Component, Layout, Node } from 'cc';
import { UIBase } from '../utils/UIFramework/UIBase';
import { BattleData } from '../Data/BattleData';
import DataManager from '../utils/DataSystem/DataManager';
import EntityManager from '../utils/EntitySystem/EntityManager';
import { StageButton } from '../Entity/stageButton';
import { ObjectPool } from '../utils/ObjectPool/ObjectPool';
import CenterLayout from '../utils/CenterLayout';
import { ProbeEvent, ProbeEventList } from '../utils/EventSystem/EventList';
const { ccclass, property } = _decorator;

@ccclass('stageSelectUI')
export class StageSelectUI extends UIBase {

    private stagesButtonPool : ObjectPool<StageButton> = new ObjectPool(this._loadButton);

    public StartBattle : ProbeEventList = new ProbeEventList();

    
    public get stages() : Node {
        return this.uiMap.GetNode("stages");
    }
    

    public async onOpen(): Promise<void> {
        let stageData = await DataManager.Instance.GetData<any[]>("StageData");
        let index = 1; 
        for (const data of stageData) {
            let button = await this.stagesButtonPool.LentItem(data,this.stages);
            button.label.string = index.toString();
            button.StageButtonDown.AddFunc(this._startBattle,this);
            index ++;
        }
        this.stages.getComponent(Layout).updateLayout(true);

    }

    public onClose(): void {
        this.stagesButtonPool.Clear();
        this.StartBattle.Clear();
    }

    private async _loadButton(){
        let button = await EntityManager.Instance.GetEntity<StageButton>("stageButton");
        return button;
    }

    private _startBattle(self : StageSelectUI,param : any[])
    {
        self.StartBattle.DoEvent(param[0])
    }
}


